<template>
  <!--输入框-->
  <a-input @click="handleOpen" v-model:value="showText" placeholder="请选择磁棒" readOnly>
    <template #prefix>
      <Icon icon="ant-design:cluster-outlined" />
    </template>
  </a-input>
  <MesReplaceModal @register="registers" @success="handleReplaceModalSuccess" />
</template>
<script lang="ts">
  import { defineComponent, ref, unref, watch } from 'vue';
  import { propTypes } from '@/utils/propTypes';
  import { useAttrs } from '@/hooks/core/useAttrs';
  import MesReplaceModal from '@/views/mes/workOrder/components/MesReplaceBachModal.vue';
  import { useModal } from '@/components/Modal';
  import { useMessage } from '@/hooks/web/useMessage';
  export default defineComponent({
    name: 'JMesReplace',
    components: {
      MesReplaceModal,
    },
    inheritAttrs: false,
    props: {
      value: propTypes.oneOfType([propTypes.string, propTypes.array]),
      row: {},
    },
    emits: ['options-change', 'change', 'select', 'update:value'],
    setup(props, { emit, refs }) {
      const attrs = useAttrs();
      const { createMessage } = useMessage();

      const getBindValue = Object.assign({}, unref(props), unref(attrs));
      const showText = ref('');
      // 打开替代料弹窗
      const [registers, { openModal }] = useModal();
      function handleOpen() {
        console.log(props);
        // 判断是否指定物料
        if ('Y' === props.row.needRawMaterialId) {
          createMessage.error('指定物料不能修改！');
          return;
        }

        // 判断是否需要磁棒
        if ('0' === props.row.needMagneticRod) {
          createMessage.error('该产品不需要磁棒！');
          return;
        }
        openModal(true, { materialId: props.row.materialId, materialName: props.row.materialSpecs, planQty: props.row.productQty, row: props.row });
      }
      /**
       * 监听value数值
       */
      watch(
        () => props.value,
        (val) => {
          showText.value = val;
        },
        { immediate: true }
      );
      function handleReplaceModalSuccess(values) {
        emit('change', values);
      }

      return {
        attrs,
        getBindValue,
        registers,
        handleOpen,
        showText,
        handleReplaceModalSuccess,
      };
    },
  });
</script>
